import csv
import Person

def read_csv():
    persons = []
    with open('data.csv','r') as f:
        reader = csv.reader(f)
        raw = list(reader)
    for p in raw[1:]:
        persons.append(Person.Person('*',p[1],p[2],p[3],p[4]))
    return persons

def group_persons_birthday(persons):
    grouped = {}
    for p in persons:
        index = str(p.birthday)
        if index not in grouped.keys():
            grouped[index] = []
        grouped[index].append(p)
    return grouped

def group_persons_zip(persons):
    grouped = {}
    for p in persons:
        index = str(p.zip)
        if index not in grouped.keys():
            grouped[index] = []
        grouped[index].append(p)
    return grouped

def get_k(grouped):
    k_dict = {}
    for index in grouped:
        k_dict[index] = len(grouped[index])
    k = min(k_dict.values())
    return k 

def anonymize_birthday(persons):
    for p in persons:
        p.birthday = p.birthday[:-1]

def anonymize_zip(persons):
    for p in persons:
        p.zip = p.zip[:-1]
def anontmize_sex(persons):
    for p in persons:
        p.sex = ''
    return persons

K = int(input('Please Input your expcet value of K:'))
persons = read_csv()
k_birthday = get_k(group_persons_birthday(persons))
k_zip = get_k(group_persons_zip(persons))
while k_birthday < K:
    anonymize_birthday(persons)
    k_birthday = get_k(group_persons_birthday(persons))

while k_zip < K:
    anonymize_zip(persons)
    k_zip = get_k(group_persons_zip(persons))

k =min (k_birthday,k_zip)
print('real value of K',k)
for p in persons:
    print(p.__str__())
